package app.v1.yd.service.impl;

import app.common.utils.YdApi;
import app.v1.yd.domain.pojo.YdWarehouse;
import app.v1.yd.mapper.YdWarehouseMapper;
import app.v1.yd.service.YdWarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author feizns
 * @since 2023/9/11
 */
@Service
@RequiredArgsConstructor
public class YdWarehouseServiceImpl implements YdWarehouseService {

    /**
     *
     */
    private final YdWarehouseMapper mapper;

    @Override
    public void updateAllWarehouseFromYiDa() {
        YdApi.warehouses().forEach(warehouse -> {
            //查询仓库
            YdWarehouse house = mapper.selectByNo(warehouse.getNo());
            //为空则新建
            if ( house == null )
                house = new YdWarehouse();
            //复制数据到仓库
            house.setNo(warehouse.getNo());
            house.setName(warehouse.getName());
            //更新或同步数据
            house.insertOrUpdate();
        });
    }

    @Override
    public List<YdWarehouse> all() {
        return mapper.selectList();
    }

}
